package com.jsjvm.client.classparser.jconstants;

import com.jsjvm.client.classparser.JClass;
import com.jsjvm.client.classparser.JConstantPool;
import com.jsjvm.client.util.DataInputStream;



public class JClassConstant implements JConstant {
	private String name;
	private JClass jClass;

	public JClassConstant(String name){
		this.name = name;
	}

	public static JConstant createConstant(int tag, DataInputStream is){
		return new JClassProxyConstant(is);
	}

	@Override
	public JConstant link(JConstantPool cpool) {
		return this;
	}

	public String getName() {
		return name;
	}
	
	public JClass getJClass() {
		return jClass;
	}
	
	public void setJClass(JClass jClass) {
		this.jClass = jClass;
	}
		
	/**
	 * Temporary class until linking is done.
	 */
	private static class JClassProxyConstant implements JConstant {
		private int nameIdx;
		public JClassProxyConstant(DataInputStream is) {
			this.nameIdx = is.readUShort();
		}
		
		private JClassConstant jcc = null;
		@Override
		public JConstant link(JConstantPool cpool) {
			if (jcc == null)
				jcc = new JClassConstant(cpool.getString(nameIdx));
			return jcc;
		}
	}
}
